package com.sf.fy.uav.applet.order.controller;

import com.sf.fy.uav.applet.order.service.OrderService;
import com.sf.fy.uav.applet.user.service.impl.AboutUserServiceImpl;
import com.sf.fy.uav.common.enums.ServicePrefixEnum;
import com.sf.fy.uav.common.response.order.PlaceOrderResponse;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.RedisPrefixKey;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.order.entity.PaySetting;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ltq
 */
@RestController
@RequestMapping("/sys/pay/")
public class PayController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisTemplate redisTemplate;

    private final static Logger logger = LoggerFactory.getLogger(PayController.class);

    @GetMapping("get-pay-settings")
    @Operation(tags = "获取支付参数", description = "获取支付参数")
    public Rs<PlaceOrderResponse> getPaySettings(@RequestParam String orderId, @RequestParam(required = false) Integer webType) {
        PaySetting paySetting = (PaySetting) redisTemplate.opsForValue().get(ServicePrefixEnum.SAME_CITY + RedisPrefixKey.PAY_SETTING + orderId);
        Integer originalWebType = paySetting.getWebType();
        if (!ObjectUtils.isEmpty(webType) && webType == 1) {
            //h5
            if (!ObjectUtils.isEmpty(originalWebType) && originalWebType == 0) {
                logger.info("当前订单来源自丰翼配送，请前往丰翼配送完成支付");
                //小程序的订单
                return Rs.failed("当前订单来源自丰翼配送，请前往丰翼配送完成支付");
            }
        } else {
            //小程序
            if (!ObjectUtils.isEmpty(originalWebType) && originalWebType == 1) {
                logger.info("当前订单来源自顺丰速运+，请前往顺丰速运+完成支付");
                //h5
                return Rs.failed("当前订单来源自顺丰速运+，请前往顺丰速运+完成支付");
            }
        }
        return Rs.success(BeanUtil.copyProperties(paySetting, PlaceOrderResponse.class));
    }
}
